package com.tju.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tju.eduservice.entity.EduSubject;
import com.tju.eduservice.entity.excel.SubjectData;
import com.tju.eduservice.entity.subject.OneSubject;
import com.tju.eduservice.entity.subject.TwoSubject;
import com.tju.eduservice.listener.SubjectExcelListener;
import com.tju.eduservice.mapper.EduSubjectMapper;
import com.tju.eduservice.service.EduSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author gsc
 * @since 2021-11-27
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class, new SubjectExcelListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getAllSubject() {
        //查询所有一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id",0);
        List<EduSubject> eduOneSubjects = baseMapper.selectList(wrapperOne);

        //查询二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id",0);
        List<EduSubject> eduTwoSubjects = baseMapper.selectList(wrapperTwo);
        // 封装一级分类
        ArrayList<OneSubject> finalSubject = new ArrayList<>();
        for (EduSubject eduOneSubject : eduOneSubjects) {
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(eduOneSubject,oneSubject);
            finalSubject.add(oneSubject);
            List<TwoSubject> children = oneSubject.getChildren();


            List<EduSubject> list1 = eduTwoSubjects.stream().filter(sub -> sub.getParentId().equals(eduOneSubject.getId())).collect(Collectors.toList());

            for (EduSubject eduSubject : list1) {
                TwoSubject twoSubject = new TwoSubject();
                BeanUtils.copyProperties(eduSubject,twoSubject);
                children.add(twoSubject);
            }

        }

//        for (EduSubject eduTwoSubject : eduTwoSubjects) {
//            for (OneSubject oneSubject : finalSubject) {
//                if(oneSubject.getId().equals(eduTwoSubject.getParentId())){
//                    List<TwoSubject> children = oneSubject.getChildren();
//                    synchronized (this){
//                        if(children == null){
//                            children = new ArrayList<>();
//                            oneSubject.setChildren(children);
//                        }
//                        children.add(new TwoSubject(eduTwoSubject.getId(),eduTwoSubject.getTitle()));
//                    }
//                }
//            }
//        }



        //封装二级分类
        return finalSubject;
    }
}
